package com.lanqiao.companymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanqiao.companymanage.entity.AssetClass;
import com.lanqiao.companymanage.entity.Dep;
import com.lanqiao.companymanage.entity.User;
import com.lanqiao.companymanage.service.StaffMessService;
import com.lanqiao.companymanage.service.UserService;
import com.lanqiao.companymanage.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 何小棍
 * 职工信息管理与添加
 */
@RestController
@RequestMapping("/departmentMess")
@Api(tags = "职工信息管理")
public class DepartmentMessController {

    @Autowired
    UserService userService;

    /**
     * 分页查询
     */
    @GetMapping("/userPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数,默认第一页"),
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10")})
    @ApiOperation(value = "职工信息分页查询")
    public Object userPage(Page<User> userPage){
        IPage<User> page =  userService.listPage(userPage);
        return Result.success(page);
    }

    /**
     * 根据员工编号(userId)查询
     * @param userId
     * @return
     */
    @GetMapping("/selectByUserId")
    @ApiOperation(value = "根据员工编号(userId)查询")
    public Object selectByUserId(Integer userId){
        User user = userService.selectByUserId(userId);
        if (user == null) {
            return Result.err("找不到对应userId的值");
        }else {
            return Result.success("查询成功",user);
        }
    }

    /**
     * 员工信息添加
     * 名称、人数、工资系数
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation(value = "员工信息添加")
    public Object addUser(User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserId,user.getUserId());
        long count = userService.count(wrapper);
        if (count>0){
            return Result.err("添加失败,userId:"+user.getUserId()+" 已经存在");
        }
        int i = userService.addUser(user);
        if (i==0) {
            return Result.err("添加失败");
        }else {
            return Result.success("添加成功",null);
        }
    }

    /**
     * 根据员工编号(userId)id删除
     */
    @GetMapping("/deletedByUserId")
    @ApiOperation(value = "根据员工编号(userId)删除")
    public Object deletedByUserId(Integer userId)  {
        int i = userService.deletedByUserId(userId);
        if (i==0) {
            return Result.err("删除失败");
        }else {
            return Result.success("删除成功",null);
        }
    }

    /**
     * 根据员工编号userId修改
     */
    @PostMapping("/updateUser")
    @ApiOperation(value = "根据员工编号userId修改")
    public Object updateUser(User user)  {
        int i = userService.updateUser(user);
        if (i==0) {
            return Result.err("修改失败");
        }else {
            return Result.success("修改成功",null);
        }
    }

    /**
     * 查询全部员工信息
     * @return
     */
    @GetMapping("getAllUser")
    @ApiOperation("查询全部员工信息")
    public Result getAllUser(){
        List<User> list =  userService.getAllUser();
        return Result.success(list);
    }



}
